<template>
  <el-dialog
    :title="!dataForm.medicineId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="药品名称" prop="medicineName">
      <el-input v-model="dataForm.medicineName" placeholder="药品名称"></el-input>
    </el-form-item>
    <el-form-item label="药品类型" prop="medicineType">
      <el-input v-model="dataForm.medicineType" placeholder="药品类型"></el-input>
    </el-form-item>
    <el-form-item label="药品图片" prop="medicineImg">
      <img v-bind:src="dataForm.medicineImg" alt="" style="width: 80px;height: 80px">
      <el-upload
        drag
        :action="url"
        :before-upload="beforeUploadHandle"
        :on-success="successHandle"
        multiple
        :file-list="fileList"
        style="text-align: center;">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只支持jpg、png、gif格式的图片！</div>
      </el-upload>
    </el-form-item>
    <el-form-item label="单价" prop="medicinePrice">
      <el-input v-model="dataForm.medicinePrice" placeholder="单价"></el-input>
    </el-form-item>
    <el-form-item label="制造商" prop="medicineManufacturer">
      <el-input v-model="dataForm.medicineManufacturer" placeholder="制造商"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="medicineNumber">
      <el-input v-model="dataForm.medicineNumber" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="介绍" prop="medicineDes">
      <el-input v-model="dataForm.medicineDes" placeholder="介绍"></el-input>
    </el-form-item>
    <el-form-item label="生产日期" prop="medicineProductionDate">
      <div class="block">
        <span class="demonstration"></span>
        <el-date-picker
          v-model="dataForm.medicineProductionDate"
          type="datetime"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="right"
          :picker-options="pickerOptions">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item label="保质期" prop="medicineQualityDate">
      <el-input v-model="dataForm.medicineQualityDate" placeholder="保质期(月)"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDatetime">
      <span class="demonstration"></span>
        <el-date-picker
          v-model="dataForm.createDatetime"
          type="datetime"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="right"
          size="large"
          :picker-options="pickerOptions">
        </el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick (picker) {
              picker.$emit('pick', new Date())
            }
          }, {
            text: '昨天',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }, {
            text: '一周前',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', date)
            }
          }]
        },
        value: new Date(),
        visible: false,
        url: '',
        dialogVisible: false,
        disabled: false,
        num: 0,
        successNum: 0,
        fileList: [],
        dataForm: {
          medicineId: 0,
          medicineName: '',
          medicineType: '',
          medicineImg: '',
          medicinePrice: '',
          medicineManufacturer: '',
          medicineNumber: '',
          medicineDes: '',
          medicineProductionDate: '',
          medicineQualityDate: '',
          createDatetime: ''
        },
        dataRule: {
          medicineName: [
            { required: true, message: '药品名称不能为空', trigger: 'blur' }
          ],
          medicineType: [
            { required: true, message: '药品类型不能为空', trigger: 'blur' }
          ],
          medicinePrice: [
            { required: true, message: '单价不能为空', trigger: 'blur' }
          ],
          medicineManufacturer: [
            { required: true, message: '制造商不能为空', trigger: 'blur' }
          ],
          medicineNumber: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          medicineDes: [
            { required: true, message: '介绍不能为空', trigger: 'blur' }
          ],
          medicineProductionDate: [
            { required: true, message: '生产日期不能为空', trigger: 'blur' }
          ],
          medicineQualityDate: [
            { required: true, message: '保质期不能为空', trigger: 'blur' }
          ],
          createDatetime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.url = this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`)
        this.dataForm.medicineId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.medicineId) {
            this.$http({
              url: this.$http.adornUrl(`/hos/medicine/info/${this.dataForm.medicineId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.medicineName = data.medicine.medicineName
                this.dataForm.medicineType = data.medicine.medicineType
                this.dataForm.medicineImg = data.medicine.medicineImg
                this.dataForm.medicinePrice = data.medicine.medicinePrice
                this.dataForm.medicineManufacturer = data.medicine.medicineManufacturer
                this.dataForm.medicineNumber = data.medicine.medicineNumber
                this.dataForm.medicineDes = data.medicine.medicineDes
                this.dataForm.medicineProductionDate = data.medicine.medicineProductionDate
                this.dataForm.medicineQualityDate = data.medicine.medicineQualityDate
                this.dataForm.createDatetime = data.medicine.createDatetime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hos/medicine/${!this.dataForm.medicineId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'medicineId': this.dataForm.medicineId || undefined,
                'medicineName': this.dataForm.medicineName,
                'medicineType': this.dataForm.medicineType,
                'medicineImg': this.dataForm.medicineImg,
                'medicinePrice': this.dataForm.medicinePrice,
                'medicineManufacturer': this.dataForm.medicineManufacturer,
                'medicineNumber': this.dataForm.medicineNumber,
                'medicineDes': this.dataForm.medicineDes,
                'medicineProductionDate': this.dataForm.medicineProductionDate,
                'medicineQualityDate': this.dataForm.medicineQualityDate,
                'createDatetime': this.dataForm.createDatetime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      beforeUploadHandle (file) {
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
        this.num++
      },
      // 上传成功
      successHandle (response, file, fileList) {
        this.fileList = fileList
        this.successNum++
        if (response && response.code === 0) {
          this.dataForm.medicineImg = response.url
          if (this.num === this.successNum) {
            this.$confirm('操作成功, 是否继续操作?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).catch(() => {
              this.visible = false
            })
          }
        } else {
          this.$message.error(response.msg)
        }
      }
    }
  }
</script>
